<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\registration;
use App\Models\time_slot;
use Illuminate\Http\Request;
use App\Models\med_doctor;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Models\nat;
use App\Models\doctor_work;

//预约管理控制器
class manage extends Controller
{
    //预约挂号
    public function registration(Request $request)
    {
        //姓名搜索
        $name=$request->get('name','');
        $type=$request->get('type','');
        //每页条数
        $limit=$request->get('limit',10);
        $mod=new registration();
        //总条数
        $count=$mod->count();
        //获取数据
        $data=$mod->show($limit,$name,$type);
        $data=json_decode(json_encode($data),true);

        if ($name!=''){
            return json_encode([
                'count'=>count($data['data']),
                'page'=>1,
                'code'=>0,
                'data'=>$data['data'],
                'msg'=>'success'
            ]);
        }
        if ($type!=''){
            return json_encode([
                'count'=>count($data['data']),
                'page'=>1,
                'code'=>0,
                'data'=>$data['data'],
                'msg'=>'success'
            ]);
        }
//        dd($data['data']);
        return json_encode([
            'count'=>$count,
            'page'=>1,
            'code'=>0,
            'data'=>$data['data'],
            'msg'=>'success'
        ]);
    }
    //删除挂号
    public function registration_del(Request $request)
    {
        $id=$request->get('id');
        $del=(new registration())->del($id);
        if ($del){
            return json_encode([
                'code'=>0,
                'msg'=>'success',
                'data'=>''
            ]);
        }else{
            return json_encode([
                'code'=>501,
                'msg'=>'error',
                'data'=>''
            ]);
        }
    }

    //预约核酸
    public function nat(Request $request)
    {
        //每页条数
        $limit=$request->get('limit',10);
        $name=$request->get('name','');
        $type=$request->get('type','');
        $mod=new nat();
        //总条数

        if ($type==''&&$name==''){
            $count=$mod->count();
        }else if ($type!=''){
            $count=$mod->where('type',$type)->count();
        }else if($name!=''){
            $count=$mod->wherehas('users',function ($res) use ($name){
                $res->where('name','like',"%$name%");
            })->count();
        }
        //获取数据
        $data=$mod->show($limit,$type,$name);
        $data=json_decode(json_encode($data),true);
//        dd($data['data']);
        return json_encode([
            'count'=>$count,
            'page'=>1,
            'code'=>0,
            'data'=>$data['data'],
            'msg'=>'success'
        ]);


    }
    //删除核酸
    public function nat_del(Request $request)
    {
        $id=$request->get('id');
        $del=(new nat())->del($id);
        if ($del){
            return json_encode([
                'code'=>0,
                'msg'=>'success',
                'data'=>''
            ]);
        }else{
            return json_encode([
                'code'=>501,
                'msg'=>'error',
                'data'=>''
            ]);
        }
    }

    //医生排班
    public function scheduling(Request $request)
    {
        $time=$request->get('time_id','time_this_slot');
        $data=(new doctor_work())->show($time);
//        $data=(new time_slot())->show();
        $data=json_decode(json_encode($data),true);


        foreach ($data as $k=>$v){
            $data[$k]['time_slot']=$v[$time];
            unset($data[$k][$time]);
        }

//        dd($data);
        $count=count($data);

        return json_encode([
            'data'=>$data,
            'code'=>0,
            'msg'=>'success',
            'count'=>$count
        ]);
    }

}
